/*
 * @Description: 请描述此文件
 * @Author: LDM
 * @Date: 2021-09-13 11:47:22
 * @LastEditors: LDM
 * @LastEditTime: 2021-09-30 17:51:10
 */
import { defineStore, acceptHMRUpdate } from 'pinia'
import { pinia } from '/@/store'

interface IavatarOptions {
  label: string //标题名称
  redirect?: string // 跳转路径
  onClick?: () => void // 点击后触发
}
interface ISettingState {
  navbarHeight: string // 导航栏高度
  sidebarWidth: string // 侧边栏高度

  showLogo: boolean // 是否显示Logo
  projectTitle: string // 项目标题
  projectLogo: string //项目Logo

  sidebar: {
    bgColor: string
    activeBgColor: string
    textColor: string
    activeTextColor: string
    openStatus: boolean
  }

  navbar: {
    avatarOptions: IavatarOptions[]
  }
}
enum sidebarWidthEnum {
  open = '210px',
  close = '64px'
}
export const useSettingStore = defineStore('setting', {
  state: (): ISettingState => ({
    showLogo: true, // 侧边栏是否显示Logo
    projectTitle: 'TS-Admin', // 项目名称
    projectLogo: '',
    navbarHeight: '60px',
    sidebarWidth: sidebarWidthEnum.open, // 侧边栏宽度
    sidebar: {
      bgColor: '#545c64', // 侧边栏背景颜色
      activeBgColor: '#434a50', // 选中子菜单颜色
      textColor: 'rgb(191, 203, 217)', //菜单文本颜色
      activeTextColor: '#409eff', // 激活时菜单文本颜色
      openStatus: true // 打开状态
    },
    navbar: {
      avatarOptions: [
        {
          label: '首页',
          redirect: '/'
        }
      ]
    }
  }),
  getters: {
    getOpenStatus(): boolean {
      return this.sidebar.openStatus
    },
    getAvatarOptions(): IavatarOptions[] {
      return this.navbar.avatarOptions
    }
  },
  actions: {
    setSidebarWidth() {
      switch (this.sidebar.openStatus) {
        case true:
          this.sidebarWidth = sidebarWidthEnum.open
          break

        default:
          this.sidebarWidth = sidebarWidthEnum.close
          break
      }
    },
    setOpenStatus(status: boolean) {
      this.sidebar.openStatus = status
      this.setSidebarWidth()
    }
  }
})

/**
 * @description: 在setup外使用仓库时调用
 * @return {*} 返回AppStore
 */
export function useSettingStoreWithOut() {
  return useSettingStore(pinia)
}

// make sure to pass the right store definition, `useAuth` in this case.
if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(useSettingStore, import.meta.hot))
}
